/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.kmcsoft.goldshop.shop;

import com.kmcsoft.emaster.entity.Employee;
import com.kmcsoft.goldshop.model.master.Shop;
import com.kmcsoft.goldshop.shop.service.ShopService;
import com.kmcsoft.spring.SpringUtil;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Set;
import org.springframework.beans.factory.annotation.Autowired;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.util.GenericForwardComposer;
import org.zkoss.zkplus.databind.AnnotateDataBinder;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listitem;

/**
 *
 * @author kmcsoft02
 */
public class AddEmpModal extends GenericForwardComposer {

    private AnnotateDataBinder binder;
    private AnnotateDataBinder paBinder;
    private Set<Employee> empls;
    private Shop selected;
    private Component component;
    private Listbox empList;
    @Autowired
    private ShopService service;

    @Override
    public void doAfterCompose(Component comp) throws Exception {
        super.doAfterCompose(comp);
        if (service == null) {
            service = SpringUtil.getBean("shopService", ShopService.class);
        }
        self.setAttribute("empModal", this);
        selected = (Shop) arg.get("shop");
        empls = selected.getEmpls();
        component = (Component) arg.get("component");
        paBinder = (AnnotateDataBinder) arg.get("binder");
        binder = new AnnotateDataBinder(self);
        self.setAttribute("binder", binder);
        binder.loadAll();
    }

    public void onClick$addEmp() {
        Set<Listitem> listitems = empList.getSelectedItems();
        for (Listitem item : listitems) {
            selected.addEmpl((Employee) item.getValue());
        }
        service.makePersistent(selected);
        paBinder.loadComponent(component);
        self.detach();
    }

    public Collection<Employee> getEmpls() {
        Collection<Employee> employees = new ArrayList<Employee>();
        employees = service.getAllEmployee();
        employees.removeAll(empls);
        return employees;
    }

    public void onClick$cancelModal() {
        self.detach();
    }
}
